package com.ymsoft.myerp.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * DTO（Data Transfer Object）：数据传输对象
 * 展示层与服务层之间的数据传输对象
 * @param <T>
 */
@Data
@ApiModel(value = "返回信息", description = "数据传输对象")
public class DTO<T> implements Serializable {

    @ApiModelProperty(value = "返回码", name = "code", example = "200", dataType = "int")
    public int code;     // 返回码
    @ApiModelProperty(value = "返回消息", name = "msg", example = "success", dataType = "String")
    public String msg;  // 返回消息
    @ApiModelProperty(value = "返回数据", name = "data", example = "", dataType = "Object")
    public T data;    // 返回数据
    @ApiModelProperty(value = "返回数量", name = "count", example = "", dataType = "int")
    public int count;
    @ApiModelProperty(value = "返回总额", name = "count", example = "", dataType = "int")
    public BigDecimal sum;

    public DTO(RetEnum retEnum, T data, int count, BigDecimal sum) {
        this.code = retEnum.getCode();
        this.msg = retEnum.getMessage();
        this.data = data;
        this.count = count;
        this.sum = sum;
    }
    public DTO(RetEnum retEnum, T data, int count) {
        this.code = retEnum.getCode();
        this.msg = retEnum.getMessage();
        this.data = data;
        this.count = count;
    }
    public DTO(RetEnum retEnum, T data) {
        this.code = retEnum.getCode();
        this.msg = retEnum.getMessage();
        this.data = data;
    }
    public DTO(RetEnum retEnum) {
        this.code = retEnum.getCode();
        this.msg = retEnum.getMessage();
        this.data = null;
    }
    public DTO(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public static DTO ret(RetEnum retEnum) {
        return new DTO(retEnum);
    }

    public static DTO ret(RetEnum retEnum, Object data) {
        return new DTO(retEnum, data);
    }

    public static DTO ret(RetEnum retEnum, Object data, int count) {
        return new DTO(retEnum, data, count);
    }

    public static DTO ret(RetEnum retEnum, Object data, int count, BigDecimal sum) {
        return new DTO(retEnum, data, count, sum);
    }

    public static DTO retSuccess(Object data, int count, BigDecimal sum) {
        return new DTO(RetEnum.SUCCESS, data, count, sum);
    }

    public static DTO retSuccess(Object data, int count) {
        return new DTO(RetEnum.SUCCESS, data, count);
    }

    public static DTO retSuccess(Object data) {
        return new DTO(RetEnum.SUCCESS, data, 0);
    }

    public static DTO retSuccess() {
        return new DTO(RetEnum.SUCCESS, null, 0);
    }

    public static DTO retFailed(Object data) {
        return new DTO(RetEnum.INNER_ERROR, data, 0);
    }

    public static DTO retFailed() {
        return new DTO(RetEnum.INNER_ERROR, null, 0);
    }
}
